fix some undefined behaviors found by -fsanitize=undefined.
authortsteven4 <tsteven4@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Sat, 9 May 2015 21:08:42 +0000 (21:08 +0000)
committertsteven4 <tsteven4@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Sat, 9 May 2015 21:08:42 +0000 (21:08 +0000)
gpsbabel/gpx.cc
gpsbabel/holux.cc
gpsbabel/saroute.cc
gpsbabel/unicsv.cc

index 5360134a0700e3b81a35fa8d0ab8774b05782c44..fd7be0c32cf883e0839f285f448433a14cd60721 100644 (file)
@@ -1850,25 +1850,33 @@ gpx_write(void)
   if (gpx_wversion_num > 10) {
     writer->writeStartElement("metadata");
   }
-  gpx_write_gdata(&gpx_global->name, "name");
-  gpx_write_gdata(&gpx_global->desc, "desc");
+  if (gpx_global) {
+    gpx_write_gdata(&gpx_global->name, "name");
+    gpx_write_gdata(&gpx_global->desc, "desc");
+  }
   /* In GPX 1.1, author changed from a string to a PersonType.
    * since it's optional, we just drop it instead of rewriting it.
    */
   if (gpx_wversion_num < 11) {
-    gpx_write_gdata(&gpx_global->author, "author");
+    if (gpx_global) {
+      gpx_write_gdata(&gpx_global->author, "author");
+    }
   }
   /* In GPX 1.1 email, url, urlname aren't allowed. */
   if (gpx_wversion_num < 11) {
-    gpx_write_gdata(&gpx_global->email, "email");
-    gpx_write_gdata(&gpx_global->url, "url");
-    gpx_write_gdata(&gpx_global->urlname, "urlname");
+    if (gpx_global) {
+      gpx_write_gdata(&gpx_global->email, "email");
+      gpx_write_gdata(&gpx_global->url, "url");
+      gpx_write_gdata(&gpx_global->urlname, "urlname");
+    }
   }
 
   gpsbabel::DateTime now = current_time();
   writer->writeTextElement("time", now.toPrettyString());
 
-  gpx_write_gdata(&gpx_global->keywords, "keywords");
+  if (gpx_global) {
+    gpx_write_gdata(&gpx_global->keywords, "keywords");
+  }
 
   gpx_write_bounds();
 
index b17c5c0aa6d365434f6662640c4b74ce78253dac..8f2aa80a80364544715c4c51b92350dc7a8aada6 100644 (file)
@@ -240,8 +240,12 @@ static void holux_disp(const Waypoint* wpt)
   }
 
 
-  le_write32(&pWptHxTmp->pt.iLatitude,(unsigned int) lat);
-  le_write32(&pWptHxTmp->pt.iLongitude,(unsigned int) lon);
+  // Note that conversions from double values to unsigned int
+  // yield undefined results for negative values.
+  // We intentionally convert to int, then do an implicit
+  // conversion to unsigned in the call.
+  le_write32(&pWptHxTmp->pt.iLatitude,(signed int) lat);
+  le_write32(&pWptHxTmp->pt.iLongitude,(signed int) lon);
   pWptHxTmp->checked = 01;
   pWptHxTmp->vocidx = (short)0xffff;
   le_write16(&((WPTHDR*)HxWFile)->num, ++sIndex);
index 0350357d4a27a8a79a5336247ba9dc9b7dc40980..8c0ce54dc69469a94735499d28cc04ef0575887b 100644 (file)
@@ -127,6 +127,7 @@ my_read(void)
     int32_t lat;
     int32_t lon;
   } *latlon;
+  struct ll mylatlon;
   uint16_t coordcount;
   route_head* track_head = NULL;
   route_head* old_track_head = NULL;
@@ -363,11 +364,13 @@ my_read(void)
 
         wpt_tmp = new Waypoint;
 
+        // copy to make sure we don't violate alignment restrictions.
+        memcpy(&mylatlon,latlon,sizeof(mylatlon));
         lat = (0x80000000UL -
-               le_read32(&latlon->lat)) /
+               le_read32(&mylatlon.lat)) /
               (double)(0x800000);
         lon = (0x80000000UL -
-               le_read32(&latlon->lon)) /
+               le_read32(&mylatlon.lon)) /
               (double)(0x800000);
 
         wpt_tmp->latitude = lat;
index b6723917ed06bbb04fe3687ba77ed9999673e6f2..72755c0699e0c326db8182fcb6be9aa099329852 100644 (file)
@@ -676,12 +676,12 @@ unicsv_parse_one_line(char* ibuf)
     switch (unicsv_fields_tab[column]) {
 
     case fld_latitude:
-      human_to_dec(CSTR(s), &wpt->latitude, &wpt->longitude, 1);
+      human_to_dec(CSTR(s), &wpt->latitude, NULL, 1);
       wpt->latitude = wpt->latitude * ns;
       break;
 
     case fld_longitude:
-      human_to_dec(CSTR(s), &wpt->latitude, &wpt->longitude, 2);
+      human_to_dec(CSTR(s), NULL, &wpt->longitude, 2);
       wpt->longitude = wpt->longitude * ew;
       break;